package org.lanqiao.tjut.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.bean.TeacherBean;
import org.lanqiao.tjut.myUtils.MyUtils;
import org.lanqiao.tjut.service.ServiceFactory;

public class ModifyTeaInfo extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 接收客户端数据请求时进行编码设置，防止中文乱码
		// request.setCharacterEncoding("UTF-8");

		// 判定当前是查询操作还是修改操作
		String strOperation = request.getParameter("opt");

		int strOpt = MyUtils.obj2Int(strOperation);

		
		if (strOpt == 0) {
			// 修改操作

			// 准备用户进行修改的数据
			// 获取id的主键值
			String strUid = request.getParameter("uid");
			// 根据主键值获取数据库中的主键对象
			TeacherBean teaBean = ServiceFactory.getUserInfoManagerSericeInstance().getTeaInfoById(strUid);
			// 将获取结果放入request缓存中
			request.setAttribute("teaBean", teaBean);
			// 跳转到修改页面
			request.getRequestDispatcher("UserInfoManager/insertT.jsp").forward(request, response);
		}
		if (strOpt == 1) {
			// 删除操作
			int strUid = MyUtils.obj2Int(request.getParameter("uid"));
			ServiceFactory.getUserInfoManagerSericeInstance().deleteTeaInfo(strUid);
			// 输入日志
			MyUtils.logger_rootLogger.info("教师用户(用户ID为"+strUid+")信息删除成功！");
			request.getRequestDispatcher("ModifyTeaInfo?opt=2").forward(request, response);
		}
		if (strOpt == 2) {
			// 查询操作
			// 输入日志
			MyUtils.logger_rootLogger.info("进行查询教师信息操作并显示...");

			// 1.获取表单数据 2.并将数据填充到实体类中
			// 获取UsersBean类的实例对象
			TeacherBean tea = (TeacherBean) MyUtils.getNewInsdence(TeacherBean.class);
			// 以下是获取表单数据

			tea.setTeaname(request.getParameter("inputUserName"));
			tea.setTeasex(request.getParameter("inputUserSex"));
			// 因为年龄为int类型而request.getParameter()获取的为String类型，进行转换
			tea.setTeaage(MyUtils.obj2Int(request.getParameter("inputUserAge")));
			tea.setTeaage2(MyUtils.obj2Int(request.getParameter("inputUserAge2")));
			tea.setTeatel(request.getParameter("inputUserTel"));
			tea.setTeaaddress(request.getParameter("inputUserAdress"));

			// 查询的用户信息集合
			List<TeacherBean> lst = ServiceFactory.getUserInfoManagerSericeInstance().getQueryTeaInfo(tea);
			// 将查询的用户信息集合放入session缓存中
			request.getSession().setAttribute("lst", lst);
			// 跳转到用户信息显示页面
			request.getRequestDispatcher("UserInfoManager/ManagerDetailsT.jsp").forward(request, response);
		}
		if (strOpt == 4) {
			// 教师修改自己信息操作

			// 准备用户进行修改的数据
			// 获取id的主键值
			String strUid = request.getParameter("uid");
			// 根据主键值获取数据库中的主键对象
			TeacherBean teaBean = ServiceFactory.getUserInfoManagerSericeInstance().getTeaInfoById(strUid);
			// 将获取结果放入request缓存中
			request.setAttribute("teaBean", teaBean);
			// 跳转到修改页面
			request.getRequestDispatcher("UserInfoManager/TeaSelfInfo.jsp").forward(request, response);
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}

}
